//
//  SecondView.swift
//  swiftTS
//
//  Created by 航电 on 2020/3/2.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

typealias secondBlock = ([String:String]) -> Void

class SecondView: UIView,UITableViewDelegate,UITableViewDataSource {

    let screenSize = UIScreen.main.bounds.size;
    var backBlock : secondBlock?;
    var mainView = UIView();
    
    lazy var mainTableView:UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: .plain);
        tableView.backgroundColor = .white;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsVerticalScrollIndicator = false;
        tableView.showsHorizontalScrollIndicator = false;
        return tableView;
    }();
    
    lazy var dataArray:NSMutableArray = {
        var dataArray = NSMutableArray();
        dataArray.add(["title":"南京夫子庙","subTitle":"南京市秦淮区秦淮河北岸中华路","latitude":"32.029171","longitude":"118.788231"]);
        dataArray.add(["title":"深圳北站","subTitle":"深圳北站优越时代广场","latitude":"22.615864","longitude":"114.035042"]);
        dataArray.add(["title":"广州人民公园","subTitle":"广州人民公园越秀艺坛","latitude":"23.133462","longitude":"113.270853"]);
        dataArray.add(["title":"福建省立医院","subTitle":"福建省立医院门诊","latitude":"26.093959","longitude":"119.313584"]);
        dataArray.add(["title":"江西省南昌航空大学","subTitle":"南昌航空大学综合实验楼","latitude":"28.657054","longitude":"115.834698"]);
        dataArray.add(["title":"浙江绍兴迪荡湖公园","subTitle":"阳光城碧桂园","latitude":"30.02052","longitude":"120.61954"]);
        dataArray.add(["title":"安徽新华学院","subTitle":"安徽新华学院男生公寓","latitude":"31.848503","longitude":"117.183702"]);
        return dataArray;
    }();
    
    public override init(frame: CGRect) {
        super.init(frame: frame);
        
        self.onCreateUIForView();
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //TODO:创建UI
    func onCreateUIForView() {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5);
        self.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height);
        self.isHidden = true;
        
        mainView = UIView(frame: CGRect(x: 200, y: 0, width: screenSize.width-200, height: self.frame.size.height));
        mainView.backgroundColor = .white;
        self.addSubview(mainView);
        
        mainView.addSubview(self.mainTableView);
        self.mainTableView.frame = mainView.bounds;
    }
    
    //TODO:Actions
    public func onShow() {
        self.isHidden = !self.isHidden;
        
    }
    
    //TODO:block回调
    func callBackBlock(secondBlock : @escaping ([String:String]) -> Void) {
        self.backBlock = secondBlock;
    }
    
    //TODO:Delegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "second_map_cell";
        var cell = tableView.dequeueReusableCell(withIdentifier: cellId);
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: cellId);
            cell?.backgroundColor = .white;
            cell?.textLabel?.textColor = .purple;
            cell?.selectionStyle = .blue;
        }
        
        if self.dataArray.count > indexPath.row {
            let data:[String:String] = self.dataArray.object(at: indexPath.row) as! [String : String];
            cell?.textLabel?.text = data["title"];
        }
        
        return cell!;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row {
            let data:[String:String] = self.dataArray.object(at: indexPath.row) as! [String : String];
            
            if self.backBlock != nil {
                self.backBlock!(data);
                
                self.isHidden = true;
            }
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch:UITouch = touches.first!;
        if touch.isKind(of: UITouch.self) {
            let touchPoint:CGPoint = touch.location(in: self);
            if !self.mainView.frame.contains(touchPoint) {
                self.isHidden = true;
            }
        }
    }
}
